import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/sales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详情'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改'
});
const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交'
});
const approveDesc = formatMessage({
    id: 'api.message.approveDesc',
    defaultMessage: '审核'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废'
});
const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});
const getManufacturerListDesc = formatMessage({
    id: 'api.message.getManufacturerListDesc',
    defaultMessage: '查询生产工厂'
});
const getProductCategoryListDesc = formatMessage({
    id: 'api.message.getProductCategoryListDesc',
    defaultMessage: '查询车型'
});
const getProductListDesc = formatMessage({
    id: 'api.message.getProductListDesc',
    defaultMessage: '查询物料组'
});
const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});
const getQuantityDesc = formatMessage({
    id: 'api.message.getQuantityDesc',
    defaultMessage: '查询环比数量'
});
const getSaleOrderForecastOrdersListDesc = formatMessage({
    id: 'api.message.getSaleOrderForecastOrdersListDesc',
    defaultMessage: '查询订单预测单'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/salePlanOrder/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/salePlanOrders?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/salePlanOrders/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const addApplyReport = data => request(`${baseUrl}/salePlanOrders`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));
//修改
export const updateApplyReport = (id, data) => request(`${baseUrl}/salePlanOrders/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 提交
export const submitData = data => request(`${baseUrl}/salePlanOrders/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));
// 审核通过
export const approvePassData = data => request(`${baseUrl}/salePlanOrders/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
// 审核拒绝
export const approveRejectData = data => request(`${baseUrl}/salePlanOrders/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
// 作废
export const abandonData = data => request(`${baseUrl}/salePlanOrders/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
// 查询生产工厂
export const getManufacturer = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/manucturers/forSalePlanOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getManufacturerListDesc));
};
// 查询车型
export const getProductCategory = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategories/forSalePlanOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getProductCategoryListDesc));
};
// 查询物料组
export const getProduct = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/products/forSalePlanOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getProductListDesc));
};
//导入模板
export const importTemp = () => request(`${baseUrl}/salePlanOrders/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
//批量导入模板
export const batchImportTemp = () => request(`${baseUrl}/salePlanOrders/batchImportTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/salePlanOrders/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
// 查询大区
export const getQuantity = data => request(`${baseUrl}/salePlanOrders/quantity`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.query, getQuantityDesc));

// 查询大区
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};

// 查询大区
export const getManufacturerSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/manucturers/forSalePlanOrder/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getManufacturerListDesc));
};
// 查询订单预测单
export const getSaleOrderForecastOrders = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleOrderForecastOrders/forSalePlanOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSaleOrderForecastOrdersListDesc));
};
